const path = require('path')

const cdn = { css: ['https://unpkg.com/element-ui/lib/theme-chalk/index.css'], js: [] }
const ispord = process.env.NODE_ENV === 'production' // 判断是否是生产环境
let externals = {}
// 如果是生产环境 就排除打包 否则不排除
if (ispord) {
  externals = {
    vue: 'Vue',
    // echarts: 'echarts',
    'element-ui': 'ELEMENT',
    'cos-js-sdk-v5': 'COS'
  }
  cdn.js = [
    'https://cdn.jsdelivr.net/npm/echarts@5.2.1/dist/echarts.min.js',
    // vue must at first!
    'https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js', // vuejs
    // element-ui js
    'https://unpkg.com/element-ui/lib/index.js', // elementUI
    'https://unpkg.com/cos-js-sdk-v5/dist/cos-js-sdk-v5.min.js'
  ] // 放置js文件目录
}

module.exports = {
  lintOnSave: false,
  chainWebpack: config => {
    const svgRule = config.module.rule('svg')
    svgRule.uses.clear()
    svgRule
      .test(/.svg$/)
      .include.add(path.resolve(__dirname, './src/icons/svg'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
    const fileRule = config.module.rule('file')
    fileRule.uses.clear()
    fileRule
      .test(/.svg$/)
      .exclude.add(path.resolve(__dirname, './src/icons/svg'))
      .end()
      .use('file-loader')
      .loader('file-loader')

    config
      .plugin('html')
      .tap(args => {
        args[0].cdn = cdn
        return args
      })
  },

  //
  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    // 打包忽略文件
    externals
  }
}
